package com.example.xymarket.httpUtil;

import android.graphics.drawable.Drawable;
import android.os.Message;

import com.example.xymarket.common.Info;
import com.example.xymarket.po.Goods;
import com.example.xymarket.po.Result;
import com.example.xymarket.common.HttpCon;
import com.example.xymarket.common.MyHandler;
import com.example.xymarket.common.MyJson;
import com.example.xymarket.po.Category;
import com.example.xymarket.common.MyUrl;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsService {

    MyHandler myHandler;

    public static Map<Integer, Drawable> imagesMap=new HashMap<>();

    public GoodsService(MyHandler myHandler1)
    {
        myHandler=myHandler1;
    }

    public void getAllCategory()  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"goods/mget_category",null,head,null);
                    Type type = new TypeToken<Result<List<Category>>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        List<Category> categoryList= (List<Category>) result.getData();
                        Message message=new Message();
                        message.obj=categoryList;
                        message.what=1;
                        myHandler.getHandler().sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }



    public void getGoodsByCategory(int i)  {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                Map<String ,String> params=new HashMap<>();
                params.put("categoryId",String.valueOf(i));
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"goods/mget_goods_by_category",params,head,null);
                    Type type = new TypeToken<Result<List<Goods>>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        List<Goods> goodsList= (List<Goods>) result.getData();
                        Message message=new Message();
                        message.obj=goodsList;
                        message.what=1;
                        myHandler.getHandler().sendMessage(message);
                    }
                    else
                    {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void getGoodsByMerchant(int i) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                Map<String ,String> params=new HashMap<>();
                params.put("merchantId",String.valueOf(i));
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"goods/mget_goods_by_merchant",params,head,null);
                    Type type = new TypeToken<Result<List<Goods>>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        List<Goods> goodsList= (List<Goods>) result.getData();
                        Message message=new Message();
                        message.obj=goodsList;
                        message.what=1;
                        myHandler.getHandler().sendMessage(message);
                    }
                    else
                    {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void getGoodsByKeyWord(String keyWord) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                Map<String ,String> params=new HashMap<>();
                params.put("keyWord",keyWord);
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"goods/msearch",params,head,null);
                    Type type = new TypeToken<Result<List<Goods>>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        List<Goods> goodsList= (List<Goods>) result.getData();
                        Message message=new Message();
                        message.obj=goodsList;
                        message.what=1;
                        myHandler.getHandler().sendMessage(message);
                    }
                    else
                    {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public void getGoodsById(Integer id) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                Map<String ,String> params=new HashMap<>();
                params.put("goodsIdList",String.valueOf(id));
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.OKPost(MyUrl.MarketUrl+"goods/mget_goods",params,head,null);
                    Type type = new TypeToken<Result<List<Goods>>>() {}.getType();
                    Result result=MyJson.getList(Json,type);
                    Integer code= result.getCode();
                    if(code==0)
                    {
                        List<Goods> goodsList= (List<Goods>) result.getData();
                        Message message=new Message();
                        message.obj=goodsList;
                        message.what=101;
                        myHandler.getHandler().sendMessage(message);
                    }
                    else
                    {
                        myHandler.getHandler().sendEmptyMessage(-101);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void InsertGoods(Map<String,Object> goodsInfo) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                Map<String ,String> head=new HashMap<>();
                head.put("Authorization", Info.getMyUserInfo().getToken());
                String Json_input=MyJson.MapToJsonString(goodsInfo);
                head.put("Authorization", Info.getMyUserInfo().getToken());
                try {
                    String Json= HttpCon.JsonPost(MyUrl.MarketUrl+"goods/save_goods",Json_input,head);
                    Integer code= MyJson.getCode(Json);
                    if(code==0)
                    {
                        myHandler.getHandler().sendEmptyMessage(100);
                    }
                    else
                    {
                        myHandler.getHandler().sendEmptyMessage(-100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
